import Home from './views/dashboard/home'
import Login from './views/login'
import Dashboard from './views/dashboard'
import Monitor from './views/dashboard/monitor'
import Electrical from './views/dashboard/electrical'
import Water from './views/dashboard/water'
import ParkingLot from './views/dashboard/parking-lot'
import Surveillance from './views/dashboard/surveillance'
import Phone from './views/dashboard/phone'
import Pm25 from './views/dashboard/PM25'
import independentPm from './views/dashboard/independentPm'
import accessControl from './views/dashboard/access-control'
import VueRouter from 'vue-router'
import Vue from 'vue'

import Test from './views/dashboard/test'

const routes = [
  {
    path: '/login',
    component: Login,
    meta: { requiresAuth: true }
  },
  {
    path: '/:id/dashboard',
    name: 'dashboard',
    component: Dashboard,
    children: [
      {
        path: 'home',
        name: 'home',
        component: Home
      },
      {
        path: 'monitor',
        name: 'monitor',
        component: Monitor
      },
      {
        path: 'electrical',
        name: 'electrical',
        component: Electrical
      },
      {
        path: 'water',
        name: 'water',
        component: Water
      },
      {
        path: 'parking-lot',
        name: 'parking-lot',
        component: ParkingLot
      },
      {
        path: 'phone',
        name: 'phone',
        component: Phone
      },
      {
        path: 'surveillance',
        name: 'surveillance',
        component: Surveillance
      },
      {
        path: 'Pm25',
        name: 'Pm25',
        component: Pm25
      },
      {
        path: 'access-control',
        name: 'access-control',
        component: accessControl
      },
      {
        path: 'independentPm',
        name: 'independentPm',
        meta: { withoutTopbar: true },
        component: independentPm
      },
      {
        path: 'test', component: Test
      }
    ]
  },

  // redirect
  { path: '/', redirect: '/login' }
]

Vue.use(VueRouter)

var router = new VueRouter({
  routes: routes
})

// 路由访问拦截
// ==============================

/**
 * 判断路由是否需要 Token 才能访问
 * 指定路由表的路由和 DEMO 站点的访问不需要Token权限
 */
// router.beforeEach((transition) => {
//   if (!window.localStorage.getItem('accessToken')) {
//     if (!isPathInRoutes(transition.to.path, authRoutes)) {
//       if (!IS_DEMO) {
//         router.replace('/login')
//       }
//     }
//   }
//   // throttle(transition)
//   transition.next()
// })

// 定义不过期的页面列表
const NO_QUIT_ROUTE_NAMES = ['Pm25-display']

router.beforeEach((to, from, next) => {
  if (!to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    if (!window.localStorage.getItem('accessToken')) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      // 强制某个页面不过期
      if (NO_QUIT_ROUTE_NAMES.indexOf(to.name) > -1) return next()
      if (new Date(window.localStorage.getItem('expiration')) < new Date()) {
        window.localStorage.setItem('expiration', new Date(window.localStorage.getItem('expirationLimit') + new Date().getTime()))
        window.localStorage.removeItem('accessToken')
        next({
          path: '/login',
          query: { redirect: to.fullPath }
        })
      } else {
        next()
      }
    }
  } else {
    next() // 确保一定要调用 next()
  }
})
export default router
